package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import pojo.Policy;

import common.RowMapper;
import common.SQLTemplate;

import dao.PolicyDAO;

public class PolicyDAOImpl extends BaseDAO implements PolicyDAO {

	@Override
	public List<Policy> getPoliciesInfoByTypeId(String policyTypeId) {
		String sql = SQLTemplate.POLICY_SELECT_JOIN_POLICY_TYPE_SQL
				+ "AND POLICY_TYPE='" + policyTypeId + "';";
		List<Policy> policiesInfo = null;
		try {
			policiesInfo = (List<Policy>) queryForList(sql,
					new PolicyInfoRowMapper());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return policiesInfo;
	}

	@Override
	public Policy getPolicyArticleById(String id) {
		String sql = SQLTemplate.POLICY_ARTICLE_SELECT_TEMPLATE + "WHERE ID='"
				+ id + "';";
		Policy policiesArticle = null;
		try {
			policiesArticle = (Policy) queryForObject(sql,
					new PolicyArticleRowMapper());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return policiesArticle;
	}

	@Override
	public Policy addPolicy(Policy policy) {
		String tableName = "T_POLICY";
		String[] columnNames = { "ID", "POLICY_TYPE_ID", "TITLE", "ARTICLE" };
		String[] values = { policy.getId(), policy.getPolicyTypeId(),
				policy.getTitle(),policy.getArticle()};
		String sql = SQLTemplate.addSQLTemplate(tableName, columnNames, values);
		try {
			insert(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return policy;
	}

	@Override
	public Policy update(Policy policy) {
		String tableName = "T_POLICY";
		String[] columnNames = {"POLICY_TYPE_ID", "TITLE", "ARTICLE" };
		String[] values = { policy.getId(), policy.getPolicyTypeId(),
				policy.getTitle(),policy.getArticle()};
		String sql = SQLTemplate.updateSQLTemplate(tableName, columnNames, values);
		try {
			update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return policy;
	}

	@Override
	public boolean deletePolicyById(String id) {
		boolean state = false;
		String sql = SQLTemplate.deleteSQLTemplate("T_POLICY") + "WHERE ID='"
				+ id + "'";
		try {
			delete(sql);
			state = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return state;
	}

	private class PolicyInfoRowMapper implements RowMapper<Object> {
		private PolicyInfoRowMapper() {
		}

		public Policy mapRow(ResultSet rs, int rowNum) throws SQLException {
			String id = rs.getString("id");
			String title = rs.getString("title");
			String time = rs.getString("time");
			String policyTypeId = rs.getString("policy_type_id");
			String policyType = rs.getString("policy_type");
			Policy policy = new Policy();
			if (id != null) {
				policy.setId(id);
			}
			if (title != null) {
				policy.setTitle(title);
			}
			if (time != null) {
				policy.setTime(time);
			}
			if (policyTypeId != null) {
				policy.setPolicyTypeId(policyTypeId);
			}
			if (policyType != null) {
				policy.setPolicyType(policyType);
			}
			return policy;
		}
	}

	private class PolicyArticleRowMapper implements RowMapper<Object> {
		private PolicyArticleRowMapper() {
		}

		public Policy mapRow(ResultSet rs, int rowNum) throws SQLException {
			String article = rs.getString("article");
			Policy policy = new Policy();
			if (article != null) {
				policy.setArticle(article);
			}
			return policy;
		}
	}
}
